<?php

/**
 * information actions.
 *
 * @package    retailbrain
 * @subpackage admin
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class informationActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->title = "Información";
        $this->breadcrumbs = array(array("Información", "information/index"));

        $id = $request->getParameter('id');
        $user = $this->getUser()->getAttribute('id_users');


        /** Stores * */
        $q = Doctrine_Query::create()
                ->from('Stores s')
                ->innerJoin('s.UserStores us')
                ->where('us.id_users = ?', array($user))
                ->orderBy('s.name');

        $this->stores = $q->fetchArray();

        $q = Doctrine_Query::create()
                ->from('Stores s')
                ->where('s.id_store = ?', array($id));
        $this->photo = $q->fetchOne();


        /** Staff * */
        $q = Doctrine_Query::create()
                ->from('StoreStaff ss')
                ->where('ss.id_store = ?', array($id))
                ->orderBy('ss.name');

        $this->staff = $q->fetchArray();


        /** Notes * */
        $q = Doctrine_Query::create()
                ->from('StoreNotes sn')
                ->innerJoin('sn.Users s')
                ->where('sn.id_store = ?', array($id))
                ->orderBy('sn.note_date desc');

        $this->notes = $q->fetchArray();


        /** Staff * */
        $q = Doctrine_Query::create()
                ->from('StorePhotos sp')
                ->where('sp.id_store = ?', array($id))
                ->limit(2)
                ->orderBy('sp.id_photo desc');

        $this->lastPhoto = $q->fetchArray();


        /** Categorys * */
        $q = Doctrine_Query::create()
                ->from('CategoryNote cn')
                ->orderBy('cn.name');

        $this->categorys = $q->fetchArray();


        /** LastCheckin * */
        $q = Doctrine_Query::create()
                ->select('s.name, sc.checkin_date')
                ->from('StoreCheckin sc')
                ->innerJoin('sc.Users s')
                ->where('sc.id_store = ?', array($id))
                ->addWhere('sc.checkin_date = (SELECT max(checkin_date) FROM StoreCheckin)');

        $lastCheckin = $q->fetchArray();

        /** LastChecklist * */
        $q = Doctrine_Query::create()
                ->select('s.name, sc.checklist_date')
                ->from('StoreChecklist sc')
                ->innerJoin('sc.Users s')
                ->where('sc.id_store = ?', array($id))
                ->addWhere('sc.checklist_date = (SELECT max(checklist_date) FROM StoreChecklist)');

        $lastChecklist = $q->fetchArray();

        /** LastCheckprice * */
        $q = Doctrine_Query::create()
                ->select('s.name, sc.checkprice_date')
                ->from('StoreCheckprice sc')
                ->innerJoin('sc.Users s')
                ->where('sc.id_store = ?', array($id))
                ->addWhere('sc.checkprice_date = (SELECT max(checkprice_date) FROM StoreCheckprice)');

        $lastCheckprice = $q->fetchArray();


        /** Visits Month * */
        $q = Doctrine_Query::create()
                ->select('count(sc.id_checkin) as visits')
                ->from('StoreCheckin sc')
                ->where('sc.id_store = ?', array($id))
                ->addWhere('MONTH(sc.checkin_date) = ?', array(date('m')))
                ->addWhere('YEAR(sc.checkin_date) = ?', array(date('Y')));

        $visitsMonth = $q->fetchArray();

        $this->indicators = array("lastCheckin" => $lastCheckin,
            "lastChecklist" => $lastChecklist,
            "lastCheckprice" => $lastCheckprice,
            "visitsMonth" => $visitsMonth);
    }

    public function executeDeleteStaff(sfWebRequest $request) {

        $id = $request->getParameter('id');
        $id_store = $request->getParameter('id_store');
        $this->forward404Unless($staff = Doctrine::getTable('StoreStaff')->find(array($id)));
        try {
            $staff->delete();
        } catch (Exception $e) {
            
        }

        $q = Doctrine_Query::create()
                ->from('StoreStaff ss')
                ->where('ss.id_store = ?', array($id_store))
                ->orderBy('ss.name');

        $this->staff = $q->fetchArray();

        $this->setTemplate('storeStaff');
    }

    public function executeAddStoreStaff(sfWebRequest $request) {

        $id_store = $request->getParameter('id_store');
        $name = $request->getParameter('name');
        $position = $request->getParameter('position');

        try {

            $tabla = Doctrine::getTable('StoreStaff');
            $tabla = new StoreStaff();

            $tabla->name = $name;
            $tabla->position = $position;
            $tabla->id_store = $id_store;

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $q = Doctrine_Query::create()
                ->from('StoreStaff ss')
                ->where('ss.id_store = ?', array($id_store))
                ->orderBy('ss.name');

        $this->staff = $q->fetchArray();

        $this->setTemplate('storeStaff');
    }

    public function executeAddStoreNotes(sfWebRequest $request) {

        $id_store = $request->getParameter('id_store');
        $category = $request->getParameter('category');
        $coments = $request->getParameter('coments');

        try {

            $tabla = Doctrine::getTable('StoreNotes');
            $tabla = new StoreNotes();

            $tabla->id_store = $id_store;
            $tabla->id_users = $this->getUser()->getAttribute('id_users');
            $tabla->id_category = $category;
            $tabla->note_date = date('Y-m-d H:m:s');
            $tabla->active = 0;
            $tabla->coments = $coments;

            $tabla->save();
        } catch (Exception $e) {
            echo $e->getMessage();
            die();
            //syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        /** Notes * */
        $q = Doctrine_Query::create()
                ->from('StoreNotes sn')
                ->innerJoin('sn.Users s')
                ->where('sn.id_store = ?', array($id_store))
                ->orderBy('sn.note_date desc');

        $this->notes = $q->fetchArray();


        /** Categorys * */
        $q = Doctrine_Query::create()
                ->from('CategoryNote cn')
                ->orderBy('cn.name');

        $this->categorys = $q->fetchArray();

        $this->setTemplate('storeNotes');
    }

}
